/*
 * Copyright (C) 2015 Bilibili
 * Copyright (C) 2015 Zhang Rui <bbcallen@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.aijle.videopaper.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.text.TextUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Preferences {
    private Context mAppContext;
    private SharedPreferences mSharedPreferences;

    private static final String videoList = "videoList";
    private static final String autoOpen = "autoOpen";
    private static final String mute = "mute";
    private static final String volume = "volume";
    private static final String opacity = "opacity";
    private static final String videoIndex = "videoIndex";
    private static final String progress = "progress";
    private static final String random = "random";
    private static final String xpos = "xpos";
    private static final String sensor = "sensor";
    private static final String showDesktop = "showDesktop";

    public Preferences(Context context) {
        mAppContext = context.getApplicationContext();
        mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(mAppContext);
    }

    public List<String> getVideoList() {
        String videoStr = mSharedPreferences.getString(videoList, "");
        if (TextUtils.isEmpty(videoStr)) {
            return new ArrayList<>();
        } else {
            return Arrays.asList(videoStr.split("\\$\\$"));
        }
    }

    public void setVideoList(List<String> value) {
        mSharedPreferences.edit().putString(videoList, TextUtils.join("$$", value)).apply();
    }

    public boolean getAutoOpen() {
        return mSharedPreferences.getBoolean(autoOpen, false);
    }

    public void setAutoOpen(boolean value) {
        mSharedPreferences.edit().putBoolean(autoOpen, value).apply();
    }

    public boolean getShowDesktop() {
        return mSharedPreferences.getBoolean(showDesktop, false);
    }

    public void setShowDesktop(boolean value) {
        mSharedPreferences.edit().putBoolean(showDesktop, value).apply();
    }

    public boolean getMute() {
        return mSharedPreferences.getBoolean(mute, false);
    }

    public void setMute(boolean value) {
        mSharedPreferences.edit().putBoolean(mute, value).apply();
    }

    public int getVolume() {
        return mSharedPreferences.getInt(volume, 100);
    }

    public void setVolume(int value) {
        mSharedPreferences.edit().putInt(volume, value).apply();
    }

    public int getOpacity() {
        return mSharedPreferences.getInt(opacity, 80);
    }

    public void setOpacity(int value) {
        mSharedPreferences.edit().putInt(opacity, value).apply();
    }

    public int getVideoIndex() {
        return mSharedPreferences.getInt(videoIndex, 0);
    }

    public void setVideoIndex(int value) {
        mSharedPreferences.edit().putInt(videoIndex, value).apply();
    }

    public int getVideoProgress() {
        return mSharedPreferences.getInt(progress, 0);
    }

    public void setVideoProgress(int value) {
        mSharedPreferences.edit().putInt(progress, value).apply();
    }

    public boolean getRandom() {
        return mSharedPreferences.getBoolean(random, false);
    }

    public void setRandom(boolean value) {
        mSharedPreferences.edit().putBoolean(random, value).apply();
    }

    public int getVideoXPos() {
        return mSharedPreferences.getInt(xpos, 50);
    }

    public void setVideoXPos(int value) {
        mSharedPreferences.edit().putInt(xpos, value).apply();
    }

    public boolean getSensor() {
        return mSharedPreferences.getBoolean(sensor, false);
    }

    public void setSensor(boolean value) {
        mSharedPreferences.edit().putBoolean(sensor, value).apply();
    }
}
